/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.widget;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.DateTime;
import lt.marsrutai.mobile.android.util.StringUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * <p>Displays the expected arrival time.</p>
 * <p>Formats and color codes the string</p>
 *
 * @author Justas
 */
public class ExpectedArrivalTimeTextView extends TextView {

    private static final int MAX_ONTIME_LATE = 2; // When late by more than this, will be red
    private static final int MAX_ONTIME_EARLY = 1; // When early by more than this, will be red
    private static final int MAX_REMAINING_MINUTES = 10; // When this or less left, report minutes

    public ExpectedArrivalTimeTextView(Context context) {
        super(context);
    }
    public ExpectedArrivalTimeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ExpectedArrivalTimeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setArrivalTime(ArrivalTime arrivalTime) {
        long delayMinutes = Math.round((arrivalTime.getExpectedTime().getTimeInMillis() - arrivalTime.getScheduledTime().getTimeInMillis())/60000.0);
        long remainingMinutes = Math.round((arrivalTime.getExpectedTime().getTimeInMillis() - new DateTime().getMills())/60000.0);

        if (!arrivalTime.getExpectedTime().equals(arrivalTime.getScheduledTime()) && remainingMinutes >= 0) {
            // Real time
            setVisibility(View.VISIBLE);

            String arrivalText = remainingMinutes > MAX_REMAINING_MINUTES ?
                    getContext().getString(R.string.arrivals_expected_time, DateTime.formatHourMinuteRounded(arrivalTime.getExpectedTime())) :
                        getContext().getString(R.string.arrivals_expected_minutes, remainingMinutes);
            String delayText = delayMinutes > MAX_ONTIME_LATE ?
                    getContext().getString(R.string.arrivals_late) : (
                            (delayMinutes < -MAX_ONTIME_EARLY) ? getContext().getString(R.string.arrivals_early) : StringUtils.EMPTY);
            String fullText = delayText.length() > 0 ?
                    StringUtils.join(", ", delayText, arrivalText) :
                    StringUtils.join(", ", getContext().getString(R.string.arrivals_ontime), arrivalText);

            setText(fullText);
            setTextColor(getContext().getResources().getColor(delayText.length() > 0 ? R.color.red : R.color.green));
        }
        else if (remainingMinutes >= 0 && remainingMinutes <= MAX_REMAINING_MINUTES) {
            // Scheduled time
            setVisibility(View.VISIBLE);
            String arrivalText = String.format(getContext().getString(R.string.arrivals_scheduled_minutes), remainingMinutes);

            setText(arrivalText);
            setTextColor(getContext().getResources().getColor(R.color.gray));
        }
        else {
            setVisibility(View.GONE);
        }
    }

}
